/*******************************************************************************
 * Licensed Materials - Property of IBM
 * (c) Copyright IBM Corporation 2005, 2008. All Rights Reserved.
 *
 * Note to U.S. Government Users Restricted Rights:  Use,
 * duplication or disclosure restricted by GSA ADP Schedule
 * Contract with IBM Corp.
 *******************************************************************************/

package org.eclipsecon.dui.interpreter.internal;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.jface.action.Action;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.dialogs.FilteredResourcesSelectionDialog;

public class BindAction extends Action {
  private final Shell fShell;
  private final ModelContainer fModelContainer;
  private final ModelContext context;

  public BindAction(ModelContainer modelContainer, ModelContext context, Shell shell) {
    super("Bind...", Activator.imageDescriptorFromPlugin(Activator.PLUGIN_ID, "icons/bind.gif"));
    fModelContainer = modelContainer;
    this.context = context;
    fShell = shell;
  }

  /*
   * @see org.eclipse.jface.action.Action#run()
   */
  @Override
  public void run() {

    String bindingPath = null;
    FilteredResourcesSelectionDialog dialog = new FilteredResourcesSelectionDialog(fShell, false, ResourcesPlugin.getWorkspace().getRoot(), IResource.FILE);
    dialog.setTitle("Open Binding Description");
    dialog.setInitialPattern("*.bind");
    dialog.setMessage("Please choose the binding description:");
    dialog.open();
    IFile file = (IFile) dialog.getFirstResult();
    if (file != null)
      bindingPath = file.getFullPath().toOSString();

    if (bindingPath == null)
      return;

    String modelPath = null;
    dialog = new FilteredResourcesSelectionDialog(fShell, false, ResourcesPlugin.getWorkspace().getRoot(), IResource.FILE);
    dialog.setTitle("Open Model");
    dialog.setInitialPattern("*.xmi");
    dialog.setMessage("Please choose the model to bind to:");
    dialog.open();
    file = (IFile) dialog.getFirstResult();

    if (file != null)
      modelPath = file.getFullPath().toOSString();

    if (modelPath == null)
      return;

    new BindingJob(fModelContainer, context, bindingPath, modelPath).schedule();
  }
}